/**
 * <p>Title: Begin.java</p>
 * <p>Description: CS 343 Final Project. This class is the main class of the program</p>
 * @author Tim Olmstead, Brady Fetting, Erik Brown, Thales Kirk
 * @email timothy.olmstead@my.uwrf.edu, brady.fetting@my.uwrf.edu, 
 * 			erik.brown@my.uwrf.edu, thales.kirk@my.uwrf.edu
 * @date November 15, 2013
 */

package Main;

import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import Components.DBManager;
import GameState.Credits;
import GameState.HelpMenu;
import GameState.Game;
import GameState.LeaderBoard;
import GameState.MainMenu;
import Settings.Config;

public class Begin extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8880429297281085242L;
	public static Begin app;
	/*
	 * Game States
	 */
	public static DBManager database;

	public static Game game;
	private static MainMenu mainmenu;
	private static LeaderBoard leaderboard;
	private static HelpMenu help;
	private static Credits credits;
	private static int gameState;
	String activeComponent = "";
	private Image icon = new ImageIcon("src/images/tire.png").getImage();

	/**
	 * @param none
	 * @description sets the bounds of the screen and begins to initiate the
	 *              game
	 */
	public Begin() {
		database = new DBManager();
		setBounds(Config.FRAME_X_LOC, Config.FRAME_Y_LOC, Config.FRAME_WIDTH,
				Config.FRAME_HEIGHT);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle(Config.TITLE);
		setResizable(false);
		setIconImage(icon);
		setGameState(0);

	}

	/**
	 * @param args
	 * @description the game's main
	 */
	public static void main(String[] args) {
		app = new Begin();
	}

	/**
	 * @param none
	 * @description gets the current gameState
	 */
	public int getGameState() {
		return gameState;
	}

	public DBManager getDatabase() {
		return database;
	}

	/**
	 * @param gameState
	 * @description sets the gameStates
	 */
	public void setGameState(int gameState) {

		// remove the active component to add a new one to the frame
		if (activeComponent.equalsIgnoreCase("game")) {
			remove(game);
		} else if (activeComponent.equalsIgnoreCase("mainmenu")) {
			remove(mainmenu);
		} else if (activeComponent.equalsIgnoreCase("help")) {
			remove(help);
		} else if (activeComponent.equalsIgnoreCase("leaderboard")) {
			remove(leaderboard);
		} else if (activeComponent.equalsIgnoreCase("credits")) {
			remove(credits);
		}

		mainmenu = null;
		game = null;
		help = null;
		leaderboard = null;

		switch (gameState) {
		case 0:
			mainmenu = new MainMenu();
			add(mainmenu);
			mainmenu.setVisible(true);
			setVisible(true);
			activeComponent = "mainmenu";
			break;
		case 1:
			game = new Game(Config.FRAME_WIDTH, Config.FRAME_HEIGHT);
			add(game);
			game.setVisible(true);
			setVisible(true);

			// sets focus on this JPanel, needed so inputs are registered
			// immediately
			game.requestFocusInWindow();

			activeComponent = "game";
			break;
		case 2:
			leaderboard = new LeaderBoard();
			add(leaderboard);
			leaderboard.setVisible(true);
			setVisible(true);
			activeComponent = "leaderboard";
			break;
		case 3:
			help = new HelpMenu();
			add(help);
			help.setVisible(true);
			setVisible(true);
			activeComponent = "help";
			break;
		case 4:
			credits = new Credits();
			add(credits);
			credits.setVisible(true);
			setVisible(true);
			activeComponent = "credits";
		}
	}
}
